package com.quanyan.place.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.place.entity.Supplier;
import com.quanyan.place.request.SupplierSearchRequest;
import com.quanyan.place.service.SupplierService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by yangyun on 2016/4/14.
 */
@RestController
//@RequestMapping("/supplier")
@RequestMapping("/admin/api/place/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    /**
     * 增加供应商
     * @param supplier
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public APIResponse<?> addSupplier(@RequestBody @Valid Supplier supplier, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return supplierService.addSupplier(supplier);
    }


    /**
     * 通过供应商ID取得供应商信息
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/get")
    public APIResponse<?> getSupplierById(@RequestParam(value="id",required =false) Integer supplierId){
        return supplierService.getSupplierById(supplierId);
    }

    /**
     * 更新供应商信息
     * @param supplier
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public APIResponse<?> updateSupplier(@RequestBody @Valid Supplier supplier, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return supplierService.addSupplier(supplier);
    }

    /**
     * 根据条件获取供应商列表
     * @param reqSupplierSearch
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public APIResponse<?> supplierList(@RequestBody @Valid SupplierSearchRequest reqSupplierSearch, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Object result = null;
        try {
            result=supplierService.supplierSearch(reqSupplierSearch);
            return APIResponse.returnSuccess(result);
        } catch (Exception e) {
            return APIResponse.returnFail(e.getMessage());
        }
    }

    /**
     * 删除供应商
     * @param supplierId
     * @return
     */
    @RequestMapping(value = "/del")
    public APIResponse<?> delSupplier(@RequestParam(value="id",required =false) Integer supplierId){
        return supplierService.deleteSupplier(supplierId);
    }

    @RequestMapping(value = "/auth", method = RequestMethod.POST)
    public APIResponse<?> authSupplierAdminUsers(@RequestBody @Valid Map<String,Object> map, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer supplierId = MapUtils.getInteger(map,"supplierId");
        List<Integer> userIds = (List<Integer>)MapUtils.getObject(map,"userIds");
        APIResponse apiResponse = null;
        try {
            apiResponse = supplierService.supplierRelateAdminUsers(supplierId,userIds);
        }catch (Exception e){
            return APIResponse.returnFail("存在已授权用户");
        }
        return apiResponse;
    }

}
